﻿using System;
using Message;
using UnityEngine;
using UnityEngine.UI;

public class RoleView : ViewBase
{
    private Transform creatPanel;
    private InputField nameField;
    private Button creatBtn;
    private Button backBtn;
    private Dropdown dropdown;

    private Transform choosePanel;
    private GameObject roleItem;
    private Button chooseRoleBtn;
    /// <summary>
    /// 创建角色按钮
    /// </summary>
    private Button startRoleBtn;

    private Button enterBtn;

    public override void Init(params object[] args)
    {
        base.Init(args);
        layer = PanelLayer.Panel;
    }

    public override void OnShowing()
    {
        base.OnShowing();
        Transform skinTrans = panelObj.transform;
        creatPanel = skinTrans.Find("CreatPanel");
        nameField = creatPanel.Find("name").GetComponent<InputField>();
        dropdown = creatPanel.Find("Dropdown").GetComponent<Dropdown>();
        creatBtn = creatPanel.Find("CreatBtn").GetComponent<Button>();
        creatBtn.onClick.AddListener(OnCreatRole);
        backBtn = creatPanel.Find("BackBtn").GetComponent<Button>();
        backBtn.onClick.AddListener(OnExitCreatRole);
        creatPanel.gameObject.SetActive(false);

        chooseRoleBtn = skinTrans.Find("chooseRoleBtn").GetComponent<Button>();
        chooseRoleBtn.onClick.AddListener(OnChoseRole);
        startRoleBtn = skinTrans.Find("startRoleBtn").GetComponent<Button>();
        startRoleBtn.onClick.AddListener(OnStartRole);

        enterBtn = skinTrans.Find("enterBtn").GetComponent<Button>();
        enterBtn.onClick.AddListener(OnEnter);

        choosePanel = skinTrans.Find("ChoosePanel");
        roleItem = choosePanel.Find("Viewport/Content/Toggle").gameObject;
        roleItem.SetActive(false);

        OnChoseRole();
    }

    /// <summary>
    /// 创建角色
    /// </summary>
    private void OnCreatRole()
    {
        if (nameField.text == "")
        {
            UIMgr.Instance.Show<TipView>("角色名不能为空!").Close(1.5f);
            return;
        }
        //注册创建角色返回结果回调
        MsgMgr.Instance.AddListener(typeof(S2CCreatRoleRet), OnCreatRoleBack);
        //发送
        C2SCreatRole protocol = new C2SCreatRole();
        protocol.uid = GlobeData.uid;
        protocol.name = nameField.text;
        protocol.type = dropdown.value;
        NetMgr.Instance.Send(protocol);
    }

    private void OnCreatRoleBack(object data)
    {
        //移除登录返回结果回调
        MsgMgr.Instance.RemoveListener(typeof(S2CCreatRoleRet), OnCreatRoleBack);

        S2CCreatRoleRet stcl = data as S2CCreatRoleRet;
        if (stcl.code == 0)
        {
            UIMgr.Instance.Show<TipView>("创建成功!").Close(1.5f);
        }
        else
        {
            UIMgr.Instance.Show<TipView>("创建失败!").Close(1.5f);
        }
    }

    private void OnExitCreatRole()
    {
        chooseRoleBtn.gameObject.SetActive(true);
        startRoleBtn.gameObject.SetActive(true);
        creatPanel.gameObject.SetActive(false);
        choosePanel.gameObject.SetActive(true);
    }

    private void OnGetRolesBack(object data)
    {
        //移除登录返回结果回调
        MsgMgr.Instance.RemoveListener(typeof(S2CGetRolesRet), OnGetRolesBack);
        Toggle[] trans = roleItem.transform.parent.GetComponentsInChildren<Toggle>(false);
        foreach (Toggle t in trans)
        {
            Destroy(t.gameObject);
        }
        GlobeData.roles.Clear();

        S2CGetRolesRet stcl = data as S2CGetRolesRet;
        if (stcl.roles.Count > 0)
        {
            for (int i = 0; i < stcl.roles.Count; i++)
            {
                Role role = stcl.roles[i];
                GlobeData.roles.Add(role);
                GameObject roleItemObj = Instantiate(roleItem, roleItem.transform.parent);
                roleItemObj.name = i.ToString();
                Text label = roleItemObj.transform.Find("Label").GetComponent<Text>();
                string ts = "";
                if (role.type == 0)
                {
                    ts = "[鬼道]";
                }
                if (role.type == 1)
                {
                    ts = "[天机]";
                }
                if (role.type == 2)
                {
                    ts = "[幻剑]";
                }
                label.text = role.name + " " + ts + " 等级 " + role.level;
                roleItemObj.SetActive(true);
                Toggle t = roleItemObj.GetComponent<Toggle>();
                t.onValueChanged.AddListener(delegate (bool isOn)
                {
                    OnValueChanged(isOn, roleItemObj);
                });
            }
        }
    }

    void OnValueChanged(bool isOn, GameObject obj)
    {
        if (isOn)
        {
            GlobeData.roleIndex = Convert.ToInt32(obj.name);
            GlobeData.uname = GlobeData.roles[GlobeData.roleIndex].name;
        }
    }

    /// <summary>
    /// 向服务器请求当前账号的所有角色
    /// </summary>
    private void OnChoseRole()
    {
        MsgMgr.Instance.AddListener(typeof(S2CGetRolesRet), OnGetRolesBack);

        C2SGetRoles protocol = new C2SGetRoles();
        protocol.uid = GlobeData.uid;
        NetMgr.Instance.Send(protocol);

        //打开选择角色面板
        choosePanel.gameObject.SetActive(true);
        creatPanel.gameObject.SetActive(false);
    }

    private void OnStartRole()
    {
        GlobeData.roleIndex = -1;
        chooseRoleBtn.gameObject.SetActive(false);
        startRoleBtn.gameObject.SetActive(false);
        creatPanel.gameObject.SetActive(true);
        choosePanel.gameObject.SetActive(false);
    }

    private void OnEnter()
    {
        if (GlobeData.roleIndex > -1)
        {
            UIMgr.Instance.Show<MainView>();
            Close();
        }
        else
        {
            UIMgr.Instance.Show<TipView>("还未选择游戏角色").Close(1.5f);
        }
    }
}